#include "../../src/server.hpp"
#include <iostream>
#include <thread>
#include <chrono>
#include <cstring>

// 测试服务器端功能
void test_server() {
    std::cout << "=== 服务器端测试 ===" << std::endl;
    
    Socket server_socket;
    
    // 创建服务器
    if (!server_socket.CreateServer(8080, "127.0.0.1", false)) {
        ERR_LOG("创建服务器失败");
        return;
    }
    
    INF_LOG("服务器启动成功，监听 127.0.0.1:8080");
    INF_LOG("等待客户端连接...");
    
    // 接受连接
    int client_fd = server_socket.accept();
    if (client_fd < 0) {
        ERR_LOG("接受连接失败");
        return;
    }
    
    // 创建客户端Socket对象来处理连接
    Socket client_socket(client_fd);
    
    // 接收数据
    char buffer[1024] = {0};
    ssize_t recv_len = client_socket.Recv(buffer, sizeof(buffer) - 1);
    if (recv_len > 0) {
        buffer[recv_len] = '\0';
        INF_LOG("收到客户端消息: %s", buffer);
        
        // 回复消息
        std::string response = "Hello from server! Received: ";
        response += buffer;
        
        ssize_t sent = client_socket.Send(response.c_str(), response.length());
        if (sent > 0) {
            INF_LOG("发送回复消息: %ld 字节", sent);
        }
    }
    
    // 关闭连接
    client_socket.Close();
    server_socket.Close();
    
    std::cout << "✅ 服务器测试完成" << std::endl;
}

// 测试客户端功能
void test_client() {
    std::cout << "=== 客户端测试 ===" << std::endl;
    
    // 等待服务器启动
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    
    Socket client_socket;
    
    // 连接服务器
    if (!client_socket.CreateClient(8080, "127.0.0.1")) {
        ERR_LOG("连接服务器失败");
        return;
    }
    
    INF_LOG("成功连接到服务器 127.0.0.1:8080");
    
    // 发送消息
    std::string message = "Hello from client!";
    ssize_t sent = client_socket.Send(message.c_str(), message.length());
    if (sent > 0) {
        INF_LOG("发送消息: %s (%ld 字节)", message.c_str(), sent);
    }
    
    // 接收回复
    char buffer[1024] = {0};
    ssize_t recv_len = client_socket.Recv(buffer, sizeof(buffer) - 1);
    if (recv_len > 0) {
        buffer[recv_len] = '\0';
        INF_LOG("收到服务器回复: %s", buffer);
    }
    
    // 关闭连接
    client_socket.Close();
    
    std::cout << "✅ 客户端测试完成" << std::endl;
}

// 测试非阻塞模式
void test_nonblock_mode() {
    std::cout << "\n=== 非阻塞模式测试 ===" << std::endl;
    
    Socket server_socket;
    
    // 创建非阻塞服务器
    if (!server_socket.CreateServer(8081, "127.0.0.1", true)) {  // true = 非阻塞
        ERR_LOG("创建非阻塞服务器失败");
        return;
    }
    
    INF_LOG("非阻塞服务器启动成功，监听 127.0.0.1:8081");
    
    // 非阻塞accept测试
    for (int i = 0; i < 5; i++) {
        int client_fd = server_socket.accept();
        if (client_fd < 0) {
            DBG_LOG("非阻塞accept：暂无连接 (第%d次尝试)", i + 1);
        } else {
            INF_LOG("非阻塞模式成功接收连接: fd=%d", client_fd);
            close(client_fd);
            break;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
    }
    
    server_socket.Close();
    std::cout << "✅ 非阻塞模式测试完成" << std::endl;
}

// 测试大数据传输
void test_large_data() {
    std::cout << "\n=== 大数据传输测试 ===" << std::endl;
    
    // 创建测试数据
    const size_t data_size = 10 * 1024;  // 10KB
    std::string large_data(data_size, 'A');
    
    // 服务器线程
    std::thread server_thread([]() {
        Socket server_socket;
        if (!server_socket.CreateServer(8082, "127.0.0.1")) {
            return;
        }
        
        int client_fd = server_socket.accept();
        if (client_fd < 0) return;
        
        Socket client_socket(client_fd);
        
        // 接收大数据
        std::string received_data;
        char buffer[1024];
        ssize_t total_received = 0;
        
        while (total_received < 10240) {  // 10KB
            ssize_t recv_len = client_socket.Recv(buffer, sizeof(buffer));
            if (recv_len <= 0) break;
            
            received_data.append(buffer, recv_len);
            total_received += recv_len;
        }
        
        INF_LOG("服务器总共接收: %ld 字节", total_received);
        
        client_socket.Close();
        server_socket.Close();
    });
    
    // 客户端
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    
    Socket client_socket;
    if (client_socket.CreateClient(8082, "127.0.0.1")) {
        // 分块发送大数据
        const size_t chunk_size = 512;
        size_t sent_total = 0;
        
        for (size_t i = 0; i < large_data.size(); i += chunk_size) {
            size_t chunk_len = std::min(chunk_size, large_data.size() - i);
            ssize_t sent = client_socket.Send(large_data.data() + i, chunk_len);
            if (sent > 0) {
                sent_total += sent;
            }
        }
        
        INF_LOG("客户端总共发送: %zu 字节", sent_total);
        client_socket.Close();
    }
    
    server_thread.join();
    std::cout << "✅ 大数据传输测试完成" << std::endl;
}

// 测试多个功能
void test_socket_features() {
    std::cout << "\n=== Socket功能特性测试 ===" << std::endl;
    
    Socket test_socket;
    
    // 测试创建套接字
    if (test_socket.create()) {
        INF_LOG("✅ 套接字创建成功");
    }
    
    // 测试地址重用设置
    test_socket.ReuseAddress();
    INF_LOG("✅ 地址重用设置成功");
    
    // 测试非阻塞设置
    test_socket.NonBlock();
    INF_LOG("✅ 非阻塞模式设置成功");
    
    // 测试关闭
    test_socket.Close();
    INF_LOG("✅ 套接字关闭成功");
    
    std::cout << "✅ Socket功能特性测试完成" << std::endl;
}

int main() {
    std::cout << "🚀 开始Socket类全面测试\n" << std::endl;
    
    // 1. 功能特性测试
    test_socket_features();
    
    // 2. 非阻塞模式测试
    test_nonblock_mode();
    
    // 3. 客户端服务器通信测试
    std::thread server_thread(test_server);
    std::thread client_thread(test_client);
    
    server_thread.join();
    client_thread.join();
    
    // 4. 大数据传输测试
    test_large_data();
    
    std::cout << "\n🎉 所有Socket测试完成！" << std::endl;
    
    return 0;
}
